using Ranorex;
using NUnit.Framework;
using System.IO;

/*
 * Copyright (c) 2007, Thomas Ardal (thomasardalatgmail.com)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of Thomas Ardal nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
namespace Ardal.MSBuildProfiler.AutomaticGuiTest
{
  [TestFixture]
  public class OpenSnapshot
  {
    [Test]
    public void OpenSnapshotTest()
    {
      MSBuildProfilerRobot robot = new MSBuildProfilerRobot();
      robot.OpenSnapshot("c:\\Home\\MSBuildProfiler\\AutomaticGuiTest\\test.snapshot");

      //Find tree view
      TreeView treeView = robot.Form.FindTreeView("treeView1");
      Assert.IsNotNull(treeView, "Could not fint tree view");
      Mouse.MoveToControl(treeView);
      treeView.Focus();

      //Assert total number of items in tree
      int itemCount = treeView.ItemCount;
      Assert.AreEqual(8, itemCount);

      //Assert build item
      int rootItem = treeView.RootItem;
      string rootItemText = treeView.GetItemText(rootItem);
      Assert.AreEqual("100 % MSBuild - 46 ms", rootItemText);

      //Assert project item
      int project = AssertChild(treeView, rootItem, @"100 % C:\Home\MSBuildProfiler\AutomaticGuiTest\build1.proj - 46 ms");

      //Assert target item
      int target1 = AssertChild(treeView, project, "66,67 % Build:Target - 31 ms");

      //Assert msbuild task
      int msbuildTask = AssertChild(treeView, target1, "33,33 % MSBuild:Task - 15 ms");

      int message1Task = treeView.GetNextItem(msbuildTask);
      Assert.IsTrue(message1Task > 0, "Child not found");
      string message1TaskText = treeView.GetItemText(message1Task);
      Assert.AreEqual("0 % Message:Task - 0 ms", message1TaskText);

      int project2 = AssertChild(treeView, msbuildTask, @"0 % C:\Home\MSBuildProfiler\AutomaticGuiTest\build2.proj - 0 ms");

      int target2 = AssertChild(treeView, project2, "0 % Build:Target - 0 ms");

      AssertChild(treeView, target2, "0 % Message:Task - 0 ms");
    }

    [Test]
    public void SaveSnapshotTest()
    {
      MSBuildProfilerRobot robot = new MSBuildProfilerRobot();
      robot.OpenSnapshot("c:\\Home\\MSBuildProfiler\\AutomaticGuiTest\\test.snapshot");
      robot.SaveSnapshot("c:\\Home\\MSBuildProfiler\\AutomaticGuiTest\\SaveSnapshotTest.snapshot");
      Assert.IsTrue(File.Exists("c:\\Home\\MSBuildProfiler\\AutomaticGuiTest\\SaveSnapshotTest.snapshot"));
      Assert.AreEqual(File.ReadAllText("c:\\Home\\MSBuildProfiler\\AutomaticGuiTest\\test.snapshot"), File.ReadAllText("c:\\Home\\MSBuildProfiler\\AutomaticGuiTest\\SaveSnapshotTest.snapshot"));
    }

    [Test]
    public void CloseSnapshotTest()
    {
      MSBuildProfilerRobot robot = new MSBuildProfilerRobot();
      robot.OpenSnapshot("c:\\Home\\MSBuildProfiler\\AutomaticGuiTest\\test.snapshot");
      robot.CloseSnapshot();

      //Find tree view
      TreeView treeView = robot.Form.FindTreeView("treeView1");
      Assert.IsNotNull(treeView, "Could not fint tree view");
      Mouse.MoveToControl(treeView);
      treeView.Focus();

      //Assert no items in tree
      int itemCount = treeView.ItemCount;
      Assert.AreEqual(0, itemCount);
    }

    private static int AssertChild(TreeView treeView, int parent, string expectedText)
    {
      int child = treeView.GetChildItem(parent);
      Assert.IsTrue(child > 0, "Child not found");
      string childText = treeView.GetItemText(child);
      Assert.AreEqual(expectedText, childText);
      return child;
    }
  }
}
